﻿// BuffDlg.cpp: 实现文件
//

#include "stdafx.h"
#include "Clicker.h"
#include "BuffDlg.h"
#include "afxdialogex.h"
#include "DefInfo/GameInfo.h"
#include "ListPublic.h"


// BuffDlg 对话框

IMPLEMENT_DYNAMIC(BuffDlg, CDialogEx)

BuffDlg::BuffDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_BUFF, pParent)
{

}

BuffDlg::~BuffDlg()
{
}

void BuffDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_BUFF, list_buff_);
	DDX_Control(pDX, IDC_LIST_DEBUFF, list_debuff_);
}


BEGIN_MESSAGE_MAP(BuffDlg, CDialogEx)
	ON_WM_TIMER()
END_MESSAGE_MAP()


// BuffDlg 消息处理程序
enum TimerEvent
{
	TE_Timer_Show,
};

void BuffDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (nIDEvent == TE_Timer_Show)
	{
		ShowInfo();
	}
	CDialogEx::OnTimer(nIDEvent);
}


BOOL BuffDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	InitList();
	SetTimer(TE_Timer_Show, 100, NULL);
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void BuffDlg::ShowInfo()
{
	if (GameInfo::inst().GetShowInfo())
	{
		ShowPlayerBuffInfo();
		ShowTargetDebuffInfo();
	}
}

void BuffDlg::ShowPlayerBuffInfo()
{
	//list_buff_.DeleteAllItems();
	for (int i = 0; i < GameInfo::inst().GetPlayerBuffCount(); i++)
	{
		auto& buff = GameInfo::inst().GetPlayerBuff(i);
		if (list_buff_.GetItemCount() <= i)
		{
			CString str;
			str.Format(L"%d", i);
			list_buff_.InsertItem(i, str);
		}
		CString str_Name;
		SpellConfig& config = GameInfo::inst().GetSpellConfigBySpellID(buff.spell_id);
		if (config.spell_id != -1)
		{
			str_Name.Format(L"%s", config.name_cn);
		}
		else
		{
			str_Name.Format(L"%d", buff.spell_id);
		}
		JudgeSetListItem(list_buff_, i, 1, str_Name);
		CString str_spell_id;
		str_spell_id.Format(L"%d", buff.spell_id);
		JudgeSetListItem(list_buff_, i, 2, str_spell_id);
		CString str_cd;
		str_cd.Format(L"%d", buff.duration);
		JudgeSetListItem(list_buff_, i, 3, str_cd);
		CString str_stack;
		str_stack.Format(L"%d", buff.stack);
		JudgeSetListItem(list_buff_, i, 4, str_stack);
	}
	if (list_buff_.GetItemCount() > GameInfo::inst().GetPlayerBuffCount())
	{
		size_t del_count = list_buff_.GetItemCount() - GameInfo::inst().GetPlayerBuffCount();
		auto del_index = GameInfo::inst().GetPlayerBuffCount();
		for (size_t i = 0; i < del_count; i++)
		{
			list_buff_.DeleteItem(list_buff_.GetItemCount() - 1);
		}
	}
	//////////////////////////////////////////////////////////////////////////
}

void BuffDlg::ShowTargetDebuffInfo()
{
	for (int i = 0; i < GameInfo::inst().GetTargetDebuffCount(); i++)
	{
		auto& buff = GameInfo::inst().GetTargetDebuff(i);
		if (list_debuff_.GetItemCount() < i)
		{
			CString str;
			str.Format(L"%d", i);
			list_debuff_.InsertItem(i, str);
		}
		CString str_Name;
		SpellConfig& config = GameInfo::inst().GetSpellConfigBySpellID(buff.spell_id);
		if (config.spell_id != -1)
		{
			str_Name.Format(L"%s", config.name_cn);
		}
		else
		{
			str_Name.Format(L"%d", buff.spell_id);
		}
		JudgeSetListItem(list_debuff_, i - 1, 1, str_Name);
		CString str_spell_id;
		str_spell_id.Format(L"%d", buff.spell_id);
		JudgeSetListItem(list_debuff_, i - 1, 2, str_spell_id);
		CString str_cd;
		str_cd.Format(L"%d", buff.duration);
		JudgeSetListItem(list_debuff_, i - 1, 3, str_cd);
		CString str_stack;
		str_stack.Format(L"%d", buff.stack);
		JudgeSetListItem(list_debuff_, i - 1, 4, str_stack);

	}
	if (list_debuff_.GetItemCount() > GameInfo::inst().GetTargetDebuffCount())
	{
		size_t del_count = list_debuff_.GetItemCount() - GameInfo::inst().GetTargetDebuffCount();
		auto del_index = GameInfo::inst().GetTargetDebuffCount();
		for (size_t i = 0; i < del_count; i++)
		{
			list_debuff_.DeleteItem(list_debuff_.GetItemCount() - 1);
		}
	}
}

void BuffDlg::InitList()
{
	//////////////////////////////////////////////////////////////////////////
	list_buff_.InsertColumn(0, L"ID", LVCFMT_LEFT, 40);//插入列 
	list_buff_.InsertColumn(1, L"名称", LVCFMT_LEFT, 100);
	list_buff_.InsertColumn(2, L"法术ID", LVCFMT_LEFT, 80);
	list_buff_.InsertColumn(3, L"时间", LVCFMT_LEFT, 40);
	list_buff_.InsertColumn(4, L"层数", LVCFMT_LEFT, 40);
	//////////////////////////////////////////////////////////////////////////
	list_debuff_.InsertColumn(0, L"ID", LVCFMT_LEFT, 40);//插入列 
	list_debuff_.InsertColumn(1, L"名称", LVCFMT_LEFT, 100);
	list_debuff_.InsertColumn(2, L"时间", LVCFMT_LEFT, 40);
	list_debuff_.InsertColumn(3, L"层数", LVCFMT_LEFT, 40);
}
